*****************************************************************************
*******************************Filepreparation******************************* 
*****************************************************************************

clear all
clear matrix
set more off, perm
capture log close
log using "C:/Userdata/Shared/Logs/workplace, elite/filepreparation.text", replace text 
cd "E:/ProjData/Workplace, elite/"


*******************************************************************************************
* This dofiles concists of the following parts:
* (1) Creating panels for elected and nominated politicians. Both in general and taking into account
* the assembly they were nominated/elected to.  
*Fairly fast file to run through
********************************************************************************************

** Create panel of politicians not taking into account to which assembly they were elected **
gen LopNr = .
gen year = .
gen nominated = .
gen elected = .
gen top = .

local elections 1982 1985 1988 1991 1994 1998 2002 2006 2010 2014 2018
foreach y in `elections' {
	foreach i in KO LT RI {
		
		merge 1:1 LopNr year using "Nom_`y'_`i'.dta", nogen update
		capture rename politiker_ politiker_`i' 								// The variable has the wrong name in the files in mod which are based on the Nominerade_KO_82_85_88_94
		replace nominated = 1 if politiker_`i' == 1
		drop politiker_`i'
		
		merge 1:1 LopNr year using "Valda_`y'_`i'.dta", nogen update
		replace elected = 1 if politiker_`i' == 1
		replace top = 1 if politiker_`i' == 1 & ("`i'" == "RI" | nrinom == 1)
		drop politiker_`i' nrinom
	}
}
su nominated elected top

** Create panel of politicians taking into account to which assembly they were elected **
foreach i in KO LT RI {
	gen nominated_`i' = .
	gen elected_`i' = .
	gen top_`i' = .
	gen list_`i' = .
}

local elections 1982 1985 1988 1991 1994 1998 2002 2006 2010 2014 2018
foreach y in `elections' {
	foreach i in KO LT RI {
		
		merge 1:1 LopNr year using "Nom_`y'_`i'.dta", nogen update
		capture rename politiker_ politiker_`i' 								// The variable has the wrong name in the files in mod which are based on the Nominerade_KO_82_85_88_94
		capture rename nrinom nrinom_`i'
		replace nominated_`i' = 1 if politiker_`i' == 1
		replace list_`i' = nrinom_`i' if nrinom_`i'!=.
		drop politiker_`i' nrinom_`ì'
		
		merge 1:1 LopNr year using "Valda_`y'_`i'.dta", nogen update
		replace elected_`i' = 1 if politiker_`i' == 1
		replace top_`i' = 1 if politiker_`i' == 1 & ("`i'" == "RI" | list_`i' == 1)
		drop politiker_`i' nrinom
	}
}
sum nominated_* elected_* top_*

** Party-specific panel: not taking into account which assembly  **
local parties C L KD M MP S V SD  
foreach p in `parties' {
	gen nominated_`p' = .
	gen elected_`p' = .
}

foreach y in `elections' {
	foreach i in KO LT RI {
	
		di "År: `y', Val: `i'"
		merge 1:1 LopNr year using "Nom_`y'_`i'.dta", nogen update keepusing(politiker_ partyabb)
		capture rename politiker_ politiker_`i'
		foreach p in `parties' {
			replace nominated_`p' = 1 if politiker_`i' == 1 & upper(partyabb) == "`p'"
		}
		drop politiker_`i' partyabb
		
		merge 1:1 LopNr year using "Valda_`y'_`i'.dta", nogen update keepusing(politiker_ partyabb)
		foreach p in `parties' {
			replace elected_`p' = 1 if politiker_`i' == 1 & upper(partyabb) == "`p'"
		}
		drop politiker_`i' partyabb 
		
	}
}



** Party-specific panel taking into account which assembly  **
local parties C L KD M MP S V SD  
foreach p in `parties' {
	gen nominated_KO_`p' = .
	gen nominated_LT_`p' = .
	gen nominated_RI_`p' = .
	
	gen elected_KO_`p' = .
	gen elected_LT_`p' = .
	gen elected_RI_`p' = .
}

foreach y in `elections' {
	foreach i in KO LT RI {
	
		di "År: `y', Val: `i'"
		merge 1:1 LopNr year using "Nom_`y'_`i'.dta", nogen update keepusing(politiker_ partyabb)
		capture rename politiker_ politiker_`i'
		foreach p in `parties' {
			replace nominated_`i'_`p' = 1 if politiker_`i' == 1 & upper(partyabb) == "`p'"
		}
		drop politiker_`i' partyabb
		
		merge 1:1 LopNr year using "Valda_`y'_`i'.dta", nogen update keepusing(politiker_ partyabb)
		foreach p in `parties' {
			replace elected_`i'_`p' = 1 if politiker_`i' == 1 & upper(partyabb) == "`p'"
		}
		drop politiker_`i' partyabb 
		
	}
}


* Clean and save
drop if elected == . & nominated == . 								
save "politicians.dta", replace
log close

